﻿//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Expenses.ExpenseService;
using Windows.System.UserProfile;
using System.ServiceModel;

namespace Expenses.ViewModels
{
    public class EmployeeViewModel : ViewModelBase
    {
        #region Properties

        private static Uri baseUri = new Uri("ms-appx:///");

        private int id = 0;
        public int Id
        {
            get
            { return id; }

            set
            {
                if (id == value)
                { return; }

                id = value;
                RaisePropertyChanged("Id");
            }
        }

        private string name = string.Empty;
        public string Name
        {
            get
            { return name; }

            set
            {
                if (name == value)
                { return; }

                name = value;
                RaisePropertyChanged("Name");
            }
        }

        private string alias = string.Empty;
        public string Alias
        {
            get
            { return alias; }

            set
            {
                if (alias == value)
                { return; }

                alias = value;
                RaisePropertyChanged("Alias");
            }
        }

        private string imagePath = string.Empty;
        public string ImagePath
        {
            get
            {
                return imagePath;
            }
            set
            {
                if (imagePath == value)
                { return; }

                imagePath = value;
                RaisePropertyChanged("ImagePath");
            }
        }

        private ImageSource image = null;
        public ImageSource Image
        {
            get
            {
                return image;
            }

            set
            {
                if (image == value)
                { return; }

                image = value;
                RaisePropertyChanged("Image");
            }
        }

        private string manager = string.Empty;
        public string Manager
        {
            get
            { return manager; }

            set
            {
                if (manager == value)
                { return; }

                manager = value;
                RaisePropertyChanged("Manager");
            }
        }

        private Employee employee = null;
        public Employee Employee
        {
            get
            {
                return employee;
            }

            set
            {
                if (employee == value)
                {
                    return;
                }

                employee = value;
                RaisePropertyChanged("Employee");
            }
        }

        #endregion "Properties"


        public async Task<Employee> GetEmployee()
        {
            var expensesService = new ExpenseServiceClient();

            string domainName = await UserInformation.GetDomainNameAsync();
            if (domainName != string.Empty)
            {
                App.DomainUser = true;
                alias = domainName.Substring(domainName.LastIndexOf("\\") + 1);
                this.Employee = await expensesService.GetEmployeeAsync(
                    alias, App.UserName);
            }
            else
            {
                // Hard code the user to Robert Green
                App.DomainUser = false;
                App.UserName = "Robert Green";
                this.Employee = await expensesService.GetEmployeeAsync(
                    "rogreen", App.UserName);
            }
            return this.Employee;
        }

        public async Task<Employee> GetEmployee(string employeeAlias)
        {
            var expensesService = new ExpenseServiceClient();
            this.Employee = await expensesService.GetEmployeeAsync(
                employeeAlias, App.UserName);
            return this.Employee;
        }

        public async Task<string> ResetData(int employeeId)
        {
            var expensesService = new ExpenseServiceClient();

            string result = await expensesService.ResetDataAsync(App.EmployeeId);
            return result;
        }

    }
}
